<?
  include "common/db_conn.php";
  
  session_start();
  
  $sm_idx = $_SESSION["sm_idx"];
  $ptype = $_POST["ptype"];
  
  echo("sm_idx[$sm_idx] ptype[$ptype]<br>");
  
  /*
    ptype
      0: 설문 응답 저장
      1: 설문 동의 저장
  */
  
  $ptype = $_REQUEST["ptype"];
  if ($ptype == "")
    $ptype = 0;
    
  if ($ptype == 0) { 
    if ($sm_idx == "") {
      echo("<script>alert('ILLEGAL ACCESS');</script>");
      exit(0);
    }
    
    // 응답 저장
    
    $count = $_POST["count"];
    
    echo("count[$count]<br>");
    
    for ($i = 0; $i < $count; $i++)
    {
      $sq_type = $_POST["sq_type_$i"];
      $sq_idx = $_POST["sq_idx_$i"];
      $sq_subtype = $_POST["sq_subtype_$i"];
      $sr_answer_count = $_POST["sr_answer_count_$i"];
      
      if ($sq_subtype == 1) { // 복수 응답형
        $sr_answer_array = $_POST["sr_answer_$i"];
        
        $query_r = "delete from sv_response where sm_idx = $sm_idx and sq_idx = $sq_idx";
        $result_r = mysql_query($query_r);

        $sr_answer_others = $_POST["sr_answer_others_$i"];
        echo("sr_answer_count[$sr_answer_count] other[$sr_answer_others]<br>");
        
        for ($m = 0; $m < count($sr_answer_array); $m++) {
          $sr_answer = $sr_answer_array[$m];
          
          if ($m == $sr_answer_count - 1) {
            $sr_answer = addslashes($sr_answer."|".$sr_answer_others);
          }
          
          echo("m[$m] sr_answer[$sr_answer]<br>");
          $query_s = "insert into sv_response (sm_idx, sq_idx, sr_answer, sr_date) values ($sm_idx, $sq_idx, '$sr_answer', now())";
          $result_s = mysql_query($query_s);
          $affected_rows_s = mysql_affected_rows();
        }
        
        
        
        
      
      } else {
        if ($sq_type == 4)
          $sr_answer = $_POST["year_$i"] * 12 + $_POST["month_$i"];
        else
          $sr_answer = $_POST["sr_answer_$i"];
          
        echo("sq_type[$sq_type] sq_idx[$sq_idx] sr_answer[$sr_answer]<br>");
        
        $query_s = "update sv_response set sr_answer = $sr_answer, sr_date = now()  where sm_idx = $sm_idx and sq_idx = $sq_idx limit 1";
        $result_s = mysql_query($query_s);
        $affected_rows_s = mysql_affected_rows();
        
        echo("update affected_rows_s[$affected_rows_s]<br>");
        
        if ($affected_rows_s == 0) {
          $query_s = "insert into sv_response (sm_idx, sq_idx, sr_answer, sr_date) values ($sm_idx, $sq_idx, $sr_answer, now())";
          $result_s = mysql_query($query_s);
          $affected_rows_s = mysql_affected_rows();
          
          echo("${query_s}.<br>");
        
          echo("insert affected_rows_s[$affected_rows_s]<br>");
        }
      }
    }
    
    $query_q = "select count(*) cnt from sv_qlist";
    $result_q = mysql_query($query_q);
    $rows_q = mysql_fetch_array($result_q);
    $quest_cnt = $rows_q["cnt"];
    
    
    $query_r = "select count(distinct sq_idx) cnt from sv_response where sm_idx = $sm_idx";
    $result_r = mysql_query($query_r);
    $rows_r = mysql_fetch_array($result_r);
    $solved_cnt = $rows_r["cnt"];
    
    echo("quest_cnt [$quest_cnt] solved_cnt[$solved_cnt]<br>");
    
?>
<script>
  if (parent.prev)
    parent.prev.style.display = "inline-block";
  if (parent.next)
    parent.next.style.display = "inline-block";
</script>
<?    
    if ($quest_cnt == $solved_cnt) {
?>
<script>
  parent.document.getElementById("confirm").style.display = "inline-block";
</script>
<?
    }
?>
<script>
  alert("저장되었습니다.");
</script>
<?  
  } else if ($ptype == 1) { // 설문 동의
    $code = $_POST["code"];
  
    $query_m = "select sm_idx, sm_code, sm_start, sm_state from sv_member where sm_code = '$code' limit 1";
    $result_m = mysql_query($query_m);
    $rows_m = mysql_fetch_array($result_m);
    
    echo("query_m[$query_m]<br>");
    
    $sm_idx = $rows_m["sm_idx"];
    $sm_code = $rows_m["sm_code"];
    $sm_start = $rows_m["sm_start"];
    $sm_state = $rows_m["sm_state"];

    $_SESSION["sm_idx"] = $sm_idx;

    // 설문 동의 
    $query_m = "update sv_member set sm_start = now() where sm_idx = $sm_idx and sm_start is NULL limit 1";
    $result_m = mysql_query($query_m);
  
    echo("query_m[$query_m]<br>");
    
    echo("sm_idx[$sm_idx]<br>");
    
    echo("<script>parent.location.href = \"survey.html?sd_idx=1\";</script>");
  } else if ($ptype == 2) { // 설문 완료
    $query_m = "update sv_member set sm_end = now(), sm_state = 1 where sm_idx = $sm_idx limit 1";
    $result_m = mysql_query($query_m);
?>
<script>
  alert("설문을 완료했습니다.");
  parent.document.location.href = "close.html";
</script>
<?
  }


  include "common/db_close.php";
?>
